<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    center>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
    <el-form-item label="文章标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="文章标题"></el-input>
    </el-form-item>
    <el-form-item label="文章副标题" prop="subTitle">
      <el-input v-model="dataForm.subTitle" placeholder="文章副标题"></el-input>
    </el-form-item>
    <el-form-item label="文章内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="文章内容"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          title: '',
          subTitle: '',
          content: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          title: [
            { required: true, message: '文章标题不能为空', trigger: 'blur' }
          ],
          subTitle: [
            { required: true, message: '文章副标题不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: `/admin/article/info/${this.dataForm.id}`,
              method: 'get'
            }).then((response) => {
              if (response && response.respCode == "200") {
                this.dataForm.title = response.data.title
                this.dataForm.subTitle = response.data.subTitle
                this.dataForm.content = response.data.content
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: `/admin/article`,
              method: this.dataForm.id == 0 ? 'post' : 'put',
              data: {
                'id': this.dataForm.id || undefined,
                'title': this.dataForm.title,
                'subTitle': this.dataForm.subTitle,
                'content': this.dataForm.content
              }
            }).then((response) => {
              if (response && response.respCode == "200") {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(response.respMessage)
              }
            })
          }
        })
      }
    }
  }
</script>
